package cn.darkhorse.meswechat.service.impl;

import cn.darkhorse.meswechat.dao.WeChatUserDao;
import cn.darkhorse.meswechat.service.IWeChatUserService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @description:
 * @author: mr.wang
 * @time: 2023/4/17 9:20
 */
@Service
public class WeChatUserServiceImpl implements IWeChatUserService {

	@Resource
	private WeChatUserDao weChatUserDao;
	
	@Override
	public void wxBindUsers(Map<String, Object> map) {
		String eventKey = String.valueOf(map.get("EventKey"));
		if(StringUtils.isNotBlank(eventKey)){
			String openId = String.valueOf(map.get("FromUserName"));
			String userId = eventKey.substring(eventKey.lastIndexOf("_")+1);
			weChatUserDao.wxBindUsers(Long.valueOf(userId),openId);
		}
		
	}
	
	@Override
	public void wxUnbindUser(Map<String, Object> map) {
		String openId = String.valueOf(map.get("FromUserName"));
		weChatUserDao.wxUnbindUser(openId);
	}
	
	@Override
	public void wxUpdateBindUser(Map<String, Object> map) {
		String eventKey = String.valueOf(map.get("EventKey"));
		if(StringUtils.isNotBlank(eventKey)){
			String openId = String.valueOf(map.get("FromUserName"));
			String userId = eventKey;
			weChatUserDao.wxBindUsers(Long.valueOf(userId),openId);
		}
	}
	
	
	@Override
	public boolean bindingState() {
		String state = weChatUserDao.bindingState(SecurityUtils.getUserId());
		return StringUtils.isBlank(state)?false:true;
	}
	
	
	
	
}
